﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructures
{
    //http://api.pullword.com/get.php
    public class Pullword : ISegmentation
    {
        public string Segment(string word)
        {
            StringBuilder sb = new StringBuilder();

            string segWord = RequestHelper.Send(SystemConfig.Get("PullWordUrl").Replace("{0}", word));
            double wordWeight = double.Parse(SystemConfig.Get("WordWeight"));
            var igonreWords = SystemConfig.Get("IgnoreWord").StringSplitToIList();

            string[] words = segWord.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in words)
            {
                string[] vals = item.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                if (vals.Length == 2)
                {
                    var val = double.Parse(vals[1]);
                    if (val > wordWeight && !igonreWords.Any(x => x == vals[0]))
                        sb.Append(vals[0] + " ");
                }
            }

            return sb.ToString();
        }
    }
}
